﻿/**
 * Created by Administrator on 2017/8/4.
 */
define(['jquery','api','AdministrativeDivisionOptionContainSelf'],function ($,api,AdministrativeDivisionOptionContainSelf) {
    var CustomerSearchOfStaff = {
        name: 'CustomerSearchOfStaff',
        template: ''
        + '<div class="company-double concluded-top"  id="department-search" v-cloak>'
        + '    <div class="concluded-top-one">'
        + '        <div class="one-input">'
        + '            <strong>时间查询：</strong>'
        + '            <strong class="state-input">'
        + '                <i-select v-model="searchInfo.searchTime" clearable>'
        + '                    <i-option v-for="item in searchTimeOption" v-bind:value="item.value" v-bind:key="item">'
        + '                        {{ item.label }}'
        + '                    </i-option>'
        + '                </i-select>'
        + '            </strong>'
        + '        </div>'
        + '        <div class="time-choice">'
        + '            <strong class="time-input">'
        + '                <Row>'
        + '                    <Col>'
        + '                    <Date-picker type="date" placeholder="选择日期" v-on:on-change="searchTimeBeginChange"  v-bind:editable="false"></Date-picker>'
        + '                    </Col>'
        + '                </Row>'
        + '            </strong>'
        + '            <strong class="zhi">至</strong>'
        + '            <strong class="time-input time-input-r">'
        + '                <Row>'
        + '                    <Col>'
        + '                    <Date-picker type="date" placeholder="选择日期" v-on:on-change="searchTimeEndChange"  v-bind:editable="false"></Date-picker>'
        + '                    </Col>'
        + '                </Row>'
        + '            </strong>'
        + '        </div>'
        + '        <div class="search-criteria-right">'
        + '            <div class="one-input" >'
        + '                <strong>省份城市：</strong>'
        + '                <strong class="state-input">'
        + '                    <Cascader v-bind:data="area" v-model="selectedArea" v-bind:render-format="format" filterable></Cascader>'
        + '                </strong>'
        + '            </div>'

        + '        <div class="ascription one-input">'
        + '            <strong>回访状态：</strong>'
        + '            <strong class="state-input">'
        + '                <i-select v-model="searchInfo.trackStatus"  clearable >'
        + '                    <i-option v-for="item in trackStatusOption" :value="item.value" :key="item">'
        + '                        {{ item.label }}'
        + '                    </i-option>'
        + '                </i-select>'
        + '            </strong>'
        + '        </div>'


        + '        </div>'
        + '    </div>'
        + '    <div class="concluded-top-two">'
        + '        <div class="one-input">'
        + '            <strong>购买意向：</strong>'
        + '            <strong class="state-input">'
        + '                <i-select v-model="searchInfo.purchasingIntention"  clearable >'
        + '                    <i-option v-for="item in purchasingIntentionOption" v-bind:value="item.value" v-bind:key="item">'
        + '                        {{ item.label }}'
        + '                    </i-option>'
        + '                </i-select>'
        + '            </strong>'
        + '        </div>'


        + '            <div class="one-input" v-if="poolType!=\'TradedCustomer\'">'
        + '                <strong>成交状态：</strong>'
        + '                <strong class="state-input">'
        + '                    <i-select v-model="searchInfo.hasTraded" clearable  style="margin-left:2%;">'
        + '                        <i-option v-for="item in tradedOption" v-bind:value="item.value" v-bind:key="item">'
        + '                            {{ item.label }}'
        + '                        </i-option>'
        + '                    </i-select>'
        + '                </strong>'
        + '            </div>'
        + '         <div class="ascription one-input"  v-if="poolType===\'TradedCustomer\'">'
        + '            <strong>客户归属：</strong>'
        + '            <strong class="state-input">'
        + '                    <Cascader v-bind:data="nodeOption" v-model="selectedNode"  filterable  change-on-select></Cascader>'
        //+ '                <i-select v-model="searchInfo.nodeId"  clearable>'
        //+ '                    <i-option v-for="item in nodeOption" :value="item.Id" :key="item.Name">{{ item.Name }}'
        //+ '                    </i-option>'
        //+ '                </i-select>'
        + '            </strong>'
        + '        </div>'

        + '        <div class="source one-input">'
        + '            <strong>来源类型：</strong>'
        + '            <strong class="state-input">'
        + '                <i-select v-model="searchInfo.sourceType"  clearable>'
        + '                    <i-option v-for="item in sourceTypeOption" :value="item" :key="item">{{ item}}</i-option>'
        + '                </i-select>'
        + '            </strong>'
        + '        </div>'
        + '        <div class="c-search-box">'
        + '            <span>关键字：</span><input type="text" class="c-input" v-model="searchInfo.searchKey" placeholder="输入手机、姓名、客户来源">'
        + '            <input type="button" class="c-search" value="搜索" v-on:click="search">'
        + '        </div>'
        + '    </div>'
        + '</div>',
        props: {
            poolType: String
        },
        data: function () {
            return {
                sortBy:"CreatedTime,false",
                searchInfo: {
                    searchTime: "",
                    searchTimeBegin: '',
                    searchTimeEnd: '',
                    hasTraded: "",
                    areaCode: "",
                    purchasingIntention: "",
                    trackStatus: "",
                    sourceType: "",
                    searchKey: "",
                    searchGroup: "",
                    sortBy: "",
                    pageIndex: 0,
                    pageSize: 0,
                    isAscending: "false",
                    nodeId:"",
                },
                searchTimeOption: [
                    {
                        value: 'AddTime',
                        label: '录入时间'
                    },
                    {
                        value: 'LastVisitTime',
                        label: '最近回访时间'
                    },
                    {
                        value: 'NextVisitTime',
                        label: '计划回访时间'
                    }
                ],
                tradedOption: [
                    {
                        value: 'true',
                        label: '已成交'
                    },
                    {
                        value: 'false',
                        label: '未成交'
                    }
                ],
                selectedArea: [],//所选的行政区域列表
                selectedNode: [],
                area: AdministrativeDivisionOptionContainSelf,//行政区域选择项
                purchasingIntentionOption: [
                    {
                        value: 'Strong',
                        label: '意向强'
                    },
                    {
                        value: 'Commonly',
                        label: '意向一般'
                    },
                    {
                        value: 'Weak',
                        label: '意向弱'
                    },
                    {
                        value: 'NoIntention',
                        label: '无意向'
                    },
                    {
                        value: 'NoVisit',
                        label: '未回访'
                    }
                ],
                trackStatusOption: [
                    {
                        value: 'WaitVisit',
                        label: '等待回访'
                    },
                    {
                        value: 'ConfirmFlight',
                        label: '确认接机'
                    },
                    {
                        value: 'ConfirmLookedHouse',
                        label: '确认带看'
                    },
                    {
                        value: 'NoVisitPlan',
                        label: '未计划'
                    }
                ],
                sourceTypeOption: [],
                nodeOption: [],
            }
        },
        computed: {

        },
        methods: {
            setSort: function () {
                var sortArr = this.sortBy.split(",");
                this.searchInfo.sortBy = sortArr[0];
                this.searchInfo.isAscending = sortArr[1];
            },
            search: function (isBack) {
                var thisSearch = this;
                thisSearch.setSort();
                api.Get_Pool_Customer_Count(thisSearch.poolType,thisSearch.searchInfo)
                    .done(function (data) {
                        if (data.IsSuccess) {
                            //触发changePage
                            window.bus.$emit("changePage", { totalRows: data.ResultObject, isBackFirstPage: isBack });//通过 bus 触发数据总数变更事件,并跳回第一页事件.
                        } else {
                            thisSearch.$Message.error(data.ErrMsg);
                        }
                    })
                    .fail(function () {
                        thisSearch.$Message.error('查询出错');
                    });
            },
            searchNext: function (Id,isNext) {
                var thisSearch = this;
                var s = {};
                $.extend(true, s, thisSearch.searchInfo);
                s.customerId = Id;
                s.isAscending = isNext ? s.isAscending : s.isAscending === "false" ? "true" : "false";

                api.Get_Pool_Customer_Next(thisSearch.poolType,s)
                    .done(function (data) {
                        if (data.IsSuccess) {
                            //触发showCustomerDetail
                            window.bus.$emit("showCustomerDetail", { customerId: data.ResultObject.Id });
                        } else {
                            thisSearch.$Message.error(data.ErrMsg);
                        }
                    })
                    .fail(function () {
                        thisSearch.$Message.error('查询出错');
                    });
            },
            exportData: function () {
                var thisSearch = this;

                api.Get_Pool_Customer_Count(thisSearch.poolType,thisSearch.searchInfo)
                    .done(function (data) {
                        if (data.IsSuccess) {
                            thisSearch.searchInfo.pageIndex = 1;
                            thisSearch.searchInfo.pageSize = data.ResultObject;
                            api.Get_Pool_Customer_List(thisSearch.poolType,thisSearch.searchInfo)
                                .done(function (data) {
                                    if (data.IsSuccess) {
                                        //触发bindExportData
                                        window.bus.$emit("bindExportData", data.ResultObject);//通过 bus 触发数据绑定事件.
                                    } else {
                                        thisSearch.$Message.error(data.ErrMsg);
                                    }
                                })
                                .fail(function () {
                                    thisSearch.$Message.error('查询出错');
                                });
                        } else {
                            thisSearch.$Message.error(data.ErrMsg);
                        }
                    })
                    .fail(function () {
                        thisSearch.$Message.error('查询出错');
                    });
            },
            searchTimeBeginChange: function (date) {
                this.searchInfo.searchTimeBegin = date;
            },
            searchTimeEndChange: function (date) {
                this.searchInfo.searchTimeEnd = date;
            },
            format: function (labels, selectedData) {
                var len = labels.length;
                if (len >= 2) {
                    var data0 = selectedData[labels.length - 1] || false;
                    var data1 = selectedData[labels.length - 2] || false;
                    if (data0 && data1 && data0.value === data1.value) {
                        len = labels.length - 1;
                    }
                }
                var label = "";
                for (var l = 0; l < len; l++) {
                    if (label === "") {
                        label = labels[l];
                    } else {
                        label = label + "/" + labels[l];
                    }
                }
                return label;
            },
        },
        created: function () {
            var thisSearch = this;
            //ajax客户来源类型
            api.Get_Customer_SourceType_Option()
                .done(function (data) {
                    if (data.IsSuccess) {
                        thisSearch.sourceTypeOption = data.ResultObject;
                    } else {
                        thisSearch.$Message.error(data.ErrMsg);
                        //alert(data.ErrMsg)
                    }
                })
                .fail(function () {
                    thisSearch.$Message.error('查询出错');
                });
            //通过bus,监听数据查询事件,监听searchData
            window.bus.$on("searchData", function (dataPage) {
                thisSearch.searchInfo.pageIndex = dataPage.pageNum;
                thisSearch.searchInfo.pageSize = dataPage.pageSize;
                api.Get_Pool_Customer_List(thisSearch.poolType,thisSearch.searchInfo)
                    .done(function (data) {
                        if (data.IsSuccess) {
                            //触发bindData
                            window.bus.$emit("bindData", data.ResultObject);//通过 bus 触发数据绑定事件.
                        } else {
                            thisSearch.$Message.error(data.ErrMsg);
                        }
                    })
                    .fail(function () {
                        thisSearch.$Message.error('查询出错');
                    });
            });
            //通过bus,监听查询事件，监听search
            window.bus.$on("search", function (isBack) {
                thisSearch.search(isBack);
            });

            //监听shiftCustomerToDeptSuccess
            window.bus.$on("shiftCustomerToDeptSuccess", function () {
                thisSearch.search(true);
            });
            //监听shiftCustomerToOtherStaffSuccess
            window.bus.$on("shiftCustomerToOtherStaffSuccess", function () {
                thisSearch.search(true);
            });

            //监听changeSort
            window.bus.$on("changeSort", function (sort) {
                thisSearch.sortBy = sort;
                thisSearch.search(true);
            });

            //监听afterExportData
            window.bus.$on("afterExportData", function () {
                thisSearch.search(true);
            });

            //监听nextCustomerDetail
            window.bus.$on("nextCustomerDetail", function (Id) {
                thisSearch.searchNext(Id,true);
            });

            //监听previousCustomerDetail
            window.bus.$on("previousCustomerDetail", function (Id) {
                thisSearch.searchNext(Id, false);
            });

            //监听checkOrSelectChange
            window.bus.$on("checkOrSelectChange", function (data) {
                thisSearch.searchInfo.searchGroup = "";
                for (var i = 0; i < data.length; i++) {
                    if (i == 0) {
                        thisSearch.searchInfo.searchGroup = data[i].value;
                    } else {
                        thisSearch.searchInfo.searchGroup = thisSearch.searchInfo.searchGroup + "," + data[i].value;
                    }
                }
            });

            //监听exportData
            window.bus.$on("exportData", function (Id) {
                thisSearch.exportData();
            });

            if (thisSearch.poolType === "TradedCustomer") {
                api.Get_Node_Tree()
                    .done(function (data) {
                        if (data.IsSuccess) {
                            thisSearch.nodeOption = [data.ResultObject];
                        } else {
                            thisSearch.$Message.error(data.ErrMsg);
                        }
                    })
                    .fail(function () {
                        thisSearch.$Message.error('查询出错');
                    });
            }
        },
        watch: {
            selectedArea: function (newArea) {
                newArea.length === 0 ? this.searchInfo.areaCode = "" : this.searchInfo.areaCode = newArea[newArea.length - 1];
            },
            selectedNode: function (newNode) {
                newNode.length === 0 ? this.searchInfo.nodeId = "" : this.searchInfo.nodeId = newNode[newNode.length - 1];
            },
        },
    };
    return CustomerSearchOfStaff;
});
